// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_isolate_ohos/flutter_isolate_ohos.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:flutter_isolate_example/main.dart';

void main() {
  testWidgets('Isolate test', (WidgetTester tester) async {
    // Ensure that the app is running
    await tester.pumpWidget(MyApp());
    await tester.pump();
    debugPrint('App started');
    final isolate = await FlutterIsolate.spawn(isolate1, "hello");
    debugPrint('spawn');
    final isolatesstart = await FlutterIsolate.runningIsolates;
    expect(isolatesstart, isNotEmpty);
    // 优雅地关闭 isolate
    await Timer(Duration(seconds: 5), () {
      debugPrint("Pausing Isolate 1");
      isolate.pause();
    });
    await Timer(Duration(seconds: 10), () {
      debugPrint("Resuming Isolate 1");
      isolate.resume();
    });
    await Timer(Duration(seconds: 20), () {
      debugPrint("Killing Isolate 1");
      isolate.kill();
    });
    await FlutterIsolate.killAll();
    final isolates = await FlutterIsolate.runningIsolates;
    expect(isolates, isEmpty);
  });
}
